
var express = require('express');
var router = express.Router();
var constants = require('./constants');

var request = constants.request;
var remote = constants.remote;

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

//登陆页面
router.get('/toLogin',function(req,res){
  res.render('login');
});

//登陆动作
router.post('/login',function(req,res){
    var loginId = req.param('loginId');
    var pass = req.param('pass');

    var queryData = {
      loginId : loginId,
      pass : pass
    };
    

    var options = {
      url:"http://"+remote.address +":"+ remote.port + "/auth/login",
      method:'POST',
      json:true,
      body:queryData,
      headers:{
        'Content-Type':'application/json; charset=UTF-8'
      }
    };


    //开始登陆
    console.log("-------->start request:",JSON.stringify(options));
  //  logger.info("-------->start request:",JSON.stringify(options));
    request(options,function(error,response,data){
        if (!error && response.statusCode == 200) {
          console.log('-------->response:',data);
    //      logger.info("-------->response:" + data);
          if(data['code'] == '1'){
            //登陆成功
            //设置session
            req.session.currentUser = data.data;

            //请求获取contact-us数据
            console.log("-------->start request:",JSON.stringify({url:"http://"+remote.address+":"+remote.port+'/contact-us/info/listAll'}));
            request({url:"http://"+remote.address+":"+remote.port+'/contact-us/info/listAll',json:true},function(error,response,data){
              if (!error && response.statusCode == 200) {

                console.log('------->contact-us response data :',data);
                //获取数据成功
                if(data['code'] == '1'){
                  //把session中的数据传给前端
                  data.currentUser = req.session.currentUser;
                  res.render('information-list',data);
                }else{

                  res.render('myerror',{message:data.msg});
                }
              }
            });
        }else{
          res.render('myerror',{message:data.msg});
        }
      }
    }
    
  );
});

router.get('/logout',function(req,res){
  if(!req.session.currentUser){
    console.log("当前未检测到session")
    res.redirect('/');
  }else{
    console.log("检测到session，",JSON.stringify(req.session.currentUser),"先清理远程session");
    request({url:"http://"+remote.address + ":" + remote.port + "/auth/logout",json:true},function(error,response,data){
      console.log(">>>>>>>>>>>>>>>>>>",error,data);
      if (!error && response.statusCode == 200) {
          console.log("远程session清理完成,开始清理本地session");
          req.session.currentUser = undefined;
          console.log("session清理完成");
          res.redirect("/");
      }else{
        res.render("error",{
          message: response.statusCode + response.statusMessage,
          error: error
      });
      }
    });
  }
});

router.get('/main-page',function(req,res){
  
});

module.exports = router;
